package com.baimifan.renovation.manager;

import android.os.Handler;
import android.os.Message;

import com.baimifan.renovation.MyApplication;
import com.baimifan.renovation.model.OneClassifyData;
import com.baimifan.renovation.model.TwoClassifyData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;

/**
 * Created by baimifan on 2017/07/04.
 */

public class OneClassifyManager {
    private Handler mhandler;
    private String url = MyApplication.MAIN + "/index/jiancai/index";
    private ArrayList<OneClassifyData> lists = new ArrayList<>();
    public ArrayList<OneClassifyData> getclass () {
        return lists;
    }

    public OneClassifyManager (Handler handler) {
        this.mhandler = handler;
    }

    public void getClassify () {
        RequestParams params = new RequestParams(url);
        x.http().post(params, new Callback.CommonCallback<JSONObject>() {
            @Override
            public void onSuccess(JSONObject result) {
                try {
                    if (result.getInt("status") == 200) {
                        JSONArray one_array = result.getJSONArray("data");
                        for (int i = 0; i< one_array.length(); i++) {
                            JSONObject one_data = one_array.getJSONObject(i);
                            OneClassifyData oneclassifydata = new OneClassifyData();
                            if (!one_data.isNull("id")) {
                                oneclassifydata.setId(one_data.getInt("id"));
                            }
                            if (!one_data.isNull("cat_name")) {
                                oneclassifydata.setCatName(one_data.getString("cat_name"));
                            }

                            if (!one_data.isNull("child")) {
                                JSONArray two_array = one_data.getJSONArray("child");
                                if (two_array != null && two_array.length() > 0) {
                                    ArrayList<TwoClassifyData> twolists = new ArrayList<TwoClassifyData>();
                                    for (int z = 0; z < two_array.length(); z++) {
                                        JSONObject two_data = two_array.getJSONObject(z);
                                        TwoClassifyData twoclassifydata = new TwoClassifyData();
                                        if (!two_data.isNull("sec_id")) {
                                            twoclassifydata.setSecId(two_data.getInt("sec_id"));
                                        }
                                        if (!two_data.isNull("img")) {
                                            twoclassifydata.setImg(MyApplication.MAIN + two_data.getString("img"));
                                        }
                                        if (!two_data.isNull("cat_name")) {
                                            twoclassifydata.setCatName(two_data.getString("cat_name"));
                                        }
                                        twolists.add(twoclassifydata);
                                    }
                                    oneclassifydata.setList(twolists);
                                }
                            }
                            lists.add(oneclassifydata);
                        }

                        Message msg = new Message();
                        msg.what = 5;
                        mhandler.sendMessage(msg);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}
